/*
 * @Descripttion: 护士获取自己的一些统计信息
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-05-06 14:19:04
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-05-06 20:31:36
 */

const DB = require('../../lib/DB')
const { Success, Fail } = require('../../lib/Response')

module.exports = async function (ctx, next) {
    try {
        // 等待护士打包
        const waiting_for_the_packaging = await DB.models.buy_medicine.count({ where: { state: '等待护士打包', nurse_id: ctx.session.nurse.id } })
        // 等待患者取药
        const wait_for_the_patient_to_get_their_medicine = await DB.models.buy_medicine.count({ where: { state: '等待患者取药', nurse_id: ctx.session.nurse.id } })
        // 仓库剩余药品数量
        const quantity_of_drugs_remaining_in_the_warehouse = await DB.models.medicine_lib.count()
        // 今日学生买药数量
        const [[{ count: the_number_of_medicine_students_buy_today }]] = await DB.query('select count(*) as count from buy_medicine where day(buy_medicine.createdAt) = day(now())')
        // 本月学生买药数量
        const [[{ count: the_number_of_medicine_students_buy_this_month }]] = await DB.query('select count(*) as count from buy_medicine where MONTH(buy_medicine.createdAt) = MONTH(now())')

        ctx.body = new Success({
            waiting_for_the_packaging,
            wait_for_the_patient_to_get_their_medicine,
            quantity_of_drugs_remaining_in_the_warehouse,
            the_number_of_medicine_students_buy_today,
            the_number_of_medicine_students_buy_this_month,
        })
        await next();
    } catch (e) {
        ctx.body = new Fail(e.message)
    }
}
